﻿
(function(a) {
    function c(c, a, b) {
        this.dec = c;
        this.group = a;
        this.neg = b
    }
    function b(a) {
        var d = ".", b = ",", e = "-";
        if (a == "us" || a == "ae" || a == "eg" || a == "il" || a == "jp" || a == "sk" || a == "th" || a == "cn" || a == "hk" || a == "tw" || a == "au" || a == "ca" || a == "gb" || a == "in") {
            d = ".";
            b = ","
        } else if (a == "de" || a == "vn" || a == "es" || a == "dk" || a == "at" || a == "gr" || a == "br") {
            d = ",";
            b = "."
        } else if (a == "cz" || a == "fr" || a == "fi" || a == "ru" || a == "se") {
            b = " ";
            d = ","
        } else if (a == "ch") {
            b = "'";
            d = "."
        }
        return new c(d, b, e)
    }
    a.formatNumber = function(f, e) {
        var e = a.extend({}, a.fn.parse.defaults, e), d = b(e.locale.toLowerCase()), h = d.dec, i = d.group, g = d.neg, c = new String(f);
        c = c.replace(".", h).replace("-", g);
        return c
    };
    a.fn.parse = function(d) {
        var d = a.extend({}, a.fn.parse.defaults, d), c = b(d.locale.toLowerCase()), i = c.dec, e = c.group, h = c.neg, g = "1234567890.-", f = [];
        this.each(function() {
            var b = new String(a(this).text());
            if (a(this).is(":input"))
                b = new String(a(this).val());
            while (b.indexOf(e) > -1)
                b = b.replace(e, "");
            b = b.replace(i, ".").replace(h, "-");
            var j = "", k = false;
            if (b.charAt(b.length - 1) == "%")
                k = true;
            for (var d = 0; d < b.length; d++)
                if (g.indexOf(b.charAt(d)) > -1)
                    j = j + b.charAt(d);
            var c = new Number(j);
            if (k) {
                c = c / 100;
                c = c.toFixed(j.length - 1)
            }
            f.push(c)
        });
        return f
    };
    a.fn.format = function(c) {
        var c = a.extend({}, a.fn.format.defaults, c), e = b(c.locale.toLowerCase()), g = e.dec, d = e.group, f = e.neg, h = "0#-,.";
        return this.each(function() {
            var o = new String(a(this).text());
            if (a(this).is(":input"))
                o = new String(a(this).val());
            for (var k = "", u = false, b = 0; b < c.format.length; b++)
                if (h.indexOf(c.format.charAt(b)) == -1)
                    k = k + c.format.charAt(b);
                else if (b == 0 && c.format.charAt(b) == "-") {
                    u = true;
                    continue
                } else
                    break;
            for (var m = "", b = c.format.length - 1; b >= 0; b--)
                if (h.indexOf(c.format.charAt(b)) == -1)
                    m = c.format.charAt(b) + m;
                else
                    break;
            c.format = c.format.substring(k.length);
            c.format = c.format.substring(0, c.format.length - m.length);
            while (o.indexOf(d) > -1)
                o = o.replace(d, "");
            var i = new Number(o.replace(g, ".").replace(f, "-"));
            if (m == "%")
                i = i * 100;
            var e = "", x = i % 1;
            if (c.format.indexOf(".") > -1) {
                var q = g, n = c.format.substring(c.format.lastIndexOf(".") + 1), j = new String(x.toFixed(n.length));
                j = j.substring(j.lastIndexOf(".") + 1);
                for (var b = 0; b < n.length; b++)
                    if (n.charAt(b) == "#" && j.charAt(b) != "0") {
                        q += j.charAt(b);
                        continue
                    } else if (n.charAt(b) == "#" && j.charAt(b) == "0") {
                        var w = j.substring(b);
                        if (w.match("[1-9]")) {
                            q += j.charAt(b);
                            continue
                        } else
                            break
                    } else if (n.charAt(b) == "0")
                        q += j.charAt(b);
                e += q
            } else
                i = Math.round(i);
            var s = Math.floor(i);
            if (i < 0)
                s = Math.ceil(i);
            var l = "";
            if (s == 0)
                l = "0";
            else {
                var p = "";
                if (c.format.indexOf(".") == -1)
                    p = c.format;
                else
                    p = c.format.substring(0, c.format.indexOf("."));
                var v = new String(Math.abs(s)), t = 9999;
                if (p.lastIndexOf(",") != -1)
                    t = p.length - p.lastIndexOf(",") - 1;
                for (var r = 0, b = v.length - 1; b > -1; b--) {
                    l = v.charAt(b) + l;
                    r++;
                    if (r == t && b != 0) {
                        l = d + l;
                        r = 0
                    }
                }
            }
            e = l + e;
            if (i < 0 && u && k.length > 0)
                k = f + k;
            else if (i < 0)
                e = f + e;
            if (!c.decimalSeparatorAlwaysShown)
                if (e.lastIndexOf(g) == e.length - 1)
                    e = e.substring(0, e.length - 1);
            e = k + e + m;
            if (a(this).is(":input"))
                a(this).val(e);
            else
                a(this).text(e)
        })
    };
    a.fn.parse.defaults = {locale: "us",decimalSeparatorAlwaysShown: false};
    a.fn.format.defaults = {format: "#,###.00",locale: "us",decimalSeparatorAlwaysShown: false}
})(jQuery)